package cn.kgc.rbac.controller;

import cn.kgc.rbac.common.CodeMsg;
import cn.kgc.rbac.common.ResponseResult;
import cn.kgc.rbac.dto.UserDTO;
import cn.kgc.rbac.dto.UserPageDTO;
import cn.kgc.rbac.entity.User;
import cn.kgc.rbac.service.UserService;
import cn.kgc.rbac.vo.UserVO;
import com.mysql.jdbc.StringUtils;
import com.sun.org.apache.bcel.internal.classfile.Code;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author: mosin
 * @version: v1.0  2023/1/30
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //login
    @RequestMapping("login")
    public ResponseResult login(UserDTO userDTO, HttpSession session){
        // 获取redis中存储得验证码
        String redisCode = redisTemplate.opsForValue().get(session.getId());
        if(redisCode==null){
            return  new ResponseResult(CodeMsg.CAPTCHA_EXPIRE);
        }else{
            if(redisCode.equals(userDTO.getCaptcha())){
                // 用户登录认证
                Subject subject = SecurityUtils.getSubject();
                UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userDTO.getUsername(), userDTO.getPassword());
                //判定用户是否勾选记住我
                if( userDTO.getRememberMe()!=null){
                    usernamePasswordToken.setRememberMe(true);
                }
                //认证  realm  ->userMapper
                subject.login(usernamePasswordToken);
                return new ResponseResult(CodeMsg.SUCCESS);
            }else{
                return  new ResponseResult(CodeMsg.CAPTCHA_ERROR);
            }
        }
    }


    // 分页查询用户信息   搜索功能
    @RequestMapping("all")
    public  ResponseResult selectPage(UserPageDTO userPageDTO){
       return userService.selectPage(userPageDTO);
    }


    // 添加用户信息
    @RequestMapping("add")
    public  ResponseResult  addUser(@RequestBody UserVO userVO){
        return userService.addUser(userVO);
    }

    //根据用户id获取用户信息

    @RequestMapping("getById")
    public ResponseResult getById(Integer id){
        return userService.getUserById(id);
    }


    //修改用户
    @RequestMapping("update")
    public  ResponseResult  updateUser(@RequestBody UserVO userVO){

      return   userService.updateUser(userVO);
    }

    //更新用户状态  user  id  state
    @RequestMapping("updateState")
    public ResponseResult  updateState(User user){
        boolean b = userService.updateById(user);
        if(b){
            return  new ResponseResult(CodeMsg.SUCCESS);
        }else{
            return  new ResponseResult(CodeMsg.FAIL);
        }
    }


    //删除用户信息
    @RequestMapping("delete")
    @RequiresRoles({"super"})
    @RequiresPermissions({"user:delete"})
    public ResponseResult deleteById(String ids){

     return  userService.deleteByUserId(ids);
    }

    //logout
    @RequestMapping("logOut")
    public ResponseResult logout(){

        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new ResponseResult(CodeMsg.SUCCESS );
    }

    //loadMenu  加载菜单
    @RequestMapping("loadMenu")
    public Map<String,Object> loadMenu(String username){
       return   userService.loadMenu(username);
    }

}
